import base64
import json

import requests
import scrapy
from settings import HTTP_PROXY
from scrapy import Request

from .utils.code_analise import base64_api


class YuncanSpider(scrapy.Spider):
    name = "yuncan"
    allowed_domains = ["palmcatering.eu"]
    start_urls = ["https://palmcatering.eu/admin/passport/login.html"]

    def start_requests(self):
        yield scrapy.Request(
            url='https://palmcatering.eu/admin/passport/captcha.html',
            callback=self.login
        )

    def login(self, response):
        code = base64_api(uname='rockly', pwd='Adcbtug0189', img=response.body, typeid=1)
        print(code)
        username = "admin"
        password = "palmnet2025"
        yield scrapy.Request(
            method='POST',
            url=self.start_urls[0],
            body=f'user_name={username}&password={password}&captcha={code}',
            callback=self.parse,
            headers={
                'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
                'Referer': 'https://palmcatering.eu/admin/passport/login.html',
                'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
                'origin': 'https://palmcatering.eu',
                'x-requested-with': 'XMLHttpRequest',
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
            },
            meta={'proxy': None}
        )


    def parse(self, response):
        print(response.text)
